//
//  WKReceiptViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/6.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKReceiptViewController.h"
//专门用来处理图片
#import <CoreImage/CoreImage.h>

@interface WKReceiptViewController ()

@property (weak, nonatomic) IBOutlet UIView *backView;//背景view
@property (weak, nonatomic) IBOutlet UIImageView *codeImageView;//二维码
@property (weak, nonatomic) IBOutlet UIButton *addressCopyBtn;//复制地址按钮
@property (weak, nonatomic) IBOutlet UIButton *saveCode;//保存二维码按钮
@property (strong, nonatomic) CIFilter *filter;//核心类: 表示滤镜

@end

@implementation WKReceiptViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setCode];
  
    
}
-(void)saveScanCode{
    UIImageWriteToSavedPhotosAlbum([self imageFromView], self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    
    DLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    if(!error) {
    //弹出保存图片是否成功提示
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"YES" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });

        
    }else{
        
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"NO" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }
    

}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = [NSString stringWithFormat:@"%@收款",self.walletModel.coinType];
    self.navigationView.backgroundColor = [UIColor clearColor];
}

- (void)layoutUI {
    self.backView.layer.cornerRadius = 10;
    self.backView.layer.masksToBounds = YES;
    
    self.addressCopyBtn.backgroundColor = AppManager.themeColor;
    self.addressCopyBtn.titleLabel.font = AppManager.btnFont;
    self.addressCopyBtn.layer.cornerRadius = 5;
    self.addressCopyBtn.layer.masksToBounds = YES;
    
    self.saveCode.layer.cornerRadius = 5;
    self.saveCode.layer.borderColor = [[UIColor blackColor] CGColor];
    self.saveCode.layer.borderWidth = 1;
    self.saveCode.backgroundColor = [UIColor whiteColor];
    self.saveCode.titleLabel.font = AppManager.btnFont;
    [self.saveCode setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

- (void)setCode {
//    //iOS系统当中提供了很多的滤镜可以使用,二维码滤镜保存在这个kCICategoryBuiltIn的category当中
//    NSArray <NSString *> * filterNames = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
//    DLog(@"%@", filterNames);
    
    //生成二维码滤镜
    self.filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //滤镜是通过KVC来设置属性, inputKeys属性来获取相关的输入key值
    NSArray <NSString *> *inputKeys = self.filter.inputKeys;
    /**
     inputMessage,
     inputCorrectionLevel
     */
    DLog(@"%@", inputKeys);

    //设置二维码内容
    NSData *data = [self.walletModel.address dataUsingEncoding:NSUTF8StringEncoding];
    [self.filter setValue:data forKey:@"inputMessage"];
    
    CIImage *ciimage = self.filter.outputImage;
    ciimage = [ciimage imageByApplyingTransform:CGAffineTransformMakeScale(10, 10)];
    DLog(@"%@", NSStringFromCGSize(ciimage.extent.size));
    
    UIImage *image = [UIImage imageWithCIImage:ciimage];
//    image = [image imageWithIcon:[UIImage imageNamed:@"dengren"]];
    self.codeImageView.image = image;


}
- (IBAction)clickSaveCode:(id)sender {
    [self saveScanCode];
}
//截取二维码区域
- (UIImage *)imageFromView
{
    
    UIGraphicsBeginImageContext(self.codeImageView.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.codeImageView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}



@end
